Ext.define('AmpleHarvest.objects.search.window.cGraph_Results',
{
	extend			: 'Ext.window.Window',
	minimizable		: false,
	maximizable		: true,
	minWidth		: 700,
	minHeight		: 500,
	width			: 700,
	height			: 500,
	shim			: false,
	animCollapse	: false,
	constrainHeader	: true,
	modal			: true,
	m_str_JsonQuery	: null,
	layout			:
	{
		type	: 'vbox',
		align	: 'stretch'
	},
	requires		:
	[
			'AmpleHarvest.Data.Store.cSolrGraph',
			"AmpleHarvest.eventHandler.cWindowEvents"
	],

	initComponent	: function()
	{
		var obj_This = this;
		var obj_CurDate = new Date();
		this.obj_Store = Ext.create('AmpleHarvest.Data.Store.cSolrGraph');

		this.m_obj_DateSelect = Ext.create('Ext.container.AbstractContainer',
		{
			layout			:
			{
				type			: 'hbox',
				defaultMargins	:
				{
					top		: 5,
					right	: 0,
					bottom	: 0,
					left	: 5
				}
			},
			m_obj_Grouping	: "publish_date",

			m_obj_EndDate	: Ext.Date.add(obj_CurDate, Ext.Date.DAY, 1),
			m_obj_StartDate	: Ext.Date.add(obj_CurDate, Ext.Date.MONTH, -12),
			frame			: true,
			items			:
			[
					{
						xtype		: 'datefield',
						fieldLabel	: "Start Date",
						flex		: 1,
						labelWidth	: 75,
						value		: Ext.Date.add(obj_CurDate, Ext.Date.MONTH,
								-12
						),
						listeners	:
						{
							'change'	:
							{
								fn		: function(obj_This, obj_New, obj_Old,
										obj_Options)
								{
									this.m_obj_DateSelect
											.set_StartDate(obj_New);
								},
								scope	: this
							},
							'render'	: function(obj_This, obj_Options)
							{
								this.m_obj_DateSelect.set_StartDate(obj_This
										.getValue());
							},
							scope		: this
						}
					},
					{
						xtype		: 'datefield',
						fieldLabel	: "End Date",
						flex		: 1,
						labelWidth	: 75,
						value		: Ext.Date
								.add(obj_CurDate, Ext.Date.DAY, 1),
						listeners	:
						{
							'change'	:
							{
								fn		: function(obj_This, obj_New, obj_Old,
										obj_Options)
								{
									this.m_obj_DateSelect.set_EndDate(obj_New);
								},
								scope	: this
							},
							'render'	:
							{
								fn		: function(obj_This, obj_Options)
								{
									this.m_obj_DateSelect.set_EndDate(obj_This
											.getValue());
								},
								scope	: this
							}
						}
					},
					{
						xtype			: 'combobox',
						flex			: 1,
						fieldLabel		: 'Grouping',
						labelWidth		: 75,
						store			: Ext.create('Ext.data.Store',
								{
									fields	:
									[
											'label', 'solrName'
									],
									data	:
									[
											{
												"label"		: "Publish Date",
												"solrName"	: "publish_date"
											},
											{
												"label"		: "Index Date",
												"solrName"	: "index_date"
											}
									]
								}),
						queryMode		: 'local',
						displayField	: 'label',
						valueField		: 'solrName',
						value			: "publish_date",
						listeners		:
						{
							'change'	:
							{
								fn		: function(obj_This, obj_New, obj_Old,
										obj_Options)
								{
									this.m_obj_DateSelect.set_Grouping(obj_New);
								},
								scope	: this
							},
							'render'	:
							{
								fn		: function(obj_This, obj_Options)
								{
									// obj_This.setValue(obj_This.m_obj_Grouping);
									this.m_obj_DateSelect.set_Grouping(obj_This
											.getValue());
								},
								scope	: this
							}
						}
					},
					{
						xtype	: "button",
						text	: "update",
						handler	: this.updateData,
						scope	: this
					}
			],
			set_StartDate	: function(obj_Date)
			{
				this.m_obj_StartDate = obj_Date;
			},
			get_StartDate	: function()
			{
				return this.m_obj_StartDate;
			},
			set_EndDate		: function(obj_Date)
			{
				this.m_obj_EndDate = obj_Date;
			},
			get_EndDate		: function()
			{
				return this.m_obj_EndDate;
			},
			set_Grouping	: function(str_Grouping)
			{
				this.m_obj_Grouping = str_Grouping;
			},
			get_Grouping	: function()
			{
				return this.m_obj_Grouping;
			}
		}
		);

		this.m_obj_Chart = Ext.create('Ext.chart.Chart',
				{
					flex	: 1,
					anchor	: "100% 100%",
					animate	: false,
					store	: this.obj_Store,
					axes	:
					[
							{
								type		: 'Numeric',
								position	: 'left',
								fields		:
								[
									'data'
								],
								label		:
								{
									renderer	: Ext.util.Format
											.numberRenderer('0,0')
								},
								title		: 'Document Count',
								grid		: true
								// ,
								// minimum : 0
							},
							{
								type		: 'Category',
								position	: 'bottom',
								fields		:
								[
									'label'
								],
								label		:
								{
									rotate	:
									{
										degrees	: 315
									}// ,
									// renderer : Ext.util.Format
									// .dateRenderer('d/m/y h:i:s')
								},
								title		: 'Date'
							}
					],
					series	:
					[
							{
								type		: 'line',
								axis		: 'left',
								highlight	: true,
								tips		:
								{
									trackMouse	: true,
									width		: 160,
									height		: 50,
									renderer	: function(storeItem, item)
									{
										this.setTitle(storeItem.get('label')
												+ '<br/> '
												+ Ext.util.Format.number(
														storeItem.get('data'),
														'0,0'
												));
									}
								},

								xField		: 'label',
								yField		: 'data'
							}
					]
				});
		this.callParent();

		if (this.m_str_JsonQuery)
		{
			this.obj_Store.setJSONQuery(this.m_str_JsonQuery);
		}

		this.obj_Store.on("beforeload", this.showMask, this);
		this.obj_Store.on("load", this.hideMask, this);
		this.on("afterrender", this.updateData, this);

		this.add(this.m_obj_DateSelect);
		this.add(this.m_obj_Chart);

	},
	showMask		: function()
	{
		if (!this.m_obj_Mask)
		{
			this.m_obj_Mask = new Ext.LoadMask(this,
					{
						msg	: "Loading Data..."
					});
		}
		this.m_obj_Mask.show();
	},
	hideMask		: function(obj_Store, arr_Records, bool_Success,
			obj_Operation, obj_Options)
	{
		if (this.m_obj_Mask)
		{
			this.m_obj_Mask.hide();
		}

		if (!bool_Success)
		{
			var obj_Temp = obj_Store.getProxy();
			if (obj_Temp)
			{
				obj_Temp = obj_Temp.getReader();
				if (obj_Temp)
				{
					obj_Temp = obj_Temp.rawData
					if (obj_Temp)
					{
						if (obj_Temp.errors)
						{
							if (obj_Temp.errors.msg)
							{
								Ext.MessageBox.alert("Error",
										"Unable to load data \n -  "
												+ obj_Temp.errors.msg
								);
								return;
							}
						}

					}
				}
			}
			Ext.MessageBox.alert("Error", "Unable to load data");
		}

	},
	updateData		: function()
	{
		var obj_StartDate = this.m_obj_DateSelect.get_StartDate();
		var obj_EndDate = this.m_obj_DateSelect.get_EndDate();
		// var arr_StartDate = new Array();
		// var arr_EndDate = new Array();
		var str_SubType = "";
		var int_DateDelta = 0;

		int_DateDelta = Ext.Date.getElapsed(obj_StartDate, obj_EndDate);

		if (int_DateDelta < (4 * 24 * 60 * 60 * 1000))
		{
			str_SubType = AmpleHarvest.Data.Store.cSolrGraph.C_STR_GROUP_DAY;
		}
		else if ((int_DateDelta < (400 * 24 * 60 * 60 * 1000)))
		{
			str_SubType = AmpleHarvest.Data.Store.cSolrGraph.C_STR_GROUP_MONTH;
		}
		else
		{
			str_SubType = AmpleHarvest.Data.Store.cSolrGraph.C_STR_GROUP_YEAR;
		}

		this.obj_Store.setStartDate(this.m_obj_DateSelect.m_obj_StartDate);
		this.obj_Store.setEndDate(this.m_obj_DateSelect.m_obj_EndDate);
		this.obj_Store.setSolrGroupField(this.m_obj_DateSelect.m_obj_Grouping);
		this.obj_Store
				.setType(AmpleHarvest.Data.Store.cSolrGraph.C_STR_TYPE_DATE);
		this.obj_Store
				.setGroup(AmpleHarvest.Data.Store.cSolrGraph.C_STR_GROUP_SOLR);

		this.obj_Store.load();
	}
}

);